const jwt = require('jsonwebtoken')
const { wxParams } = require('../libs/config')

async function verificationToken(token = '') {
    if (!token) {
        let data = {
            code: 1403,
            data: {},
            msg: 'token过期，请重新登录'
        }
        return data
    } else {
        let ret = await _jwtVerify(token)
        if (typeof (ret.name) == 'number') {
            return {
                code: 0
            }
        } else {
            let data = {
                code: 1403,
                data: {},
                msg: 'token过期，请重新登录'
            }
            return data
        }
    }
}

async function _jwtVerify(t) {
    let result = jwt.verify(t, wxParams.jwtSecret, (err, decoded) => {
        if (err) return err
        if (decoded) return decoded
    })
    return result
}


module.exports = {
    verificationToken: verificationToken
}